var crud = require('../../util/crud');
var model = require('../../model/index.js');
var Class = model.get('Class');
var response = require('../../util/response');
var modules = {};

modules.create = (req, res) => {
   
    Class.create(req.body).then((classObj) => {
    	return res.json(response.success({data: classObj}))
    }).catch(err => {
        console.log('classObj create err, err = ' + err);
        return res.json(response.error('新建班级失败')).end();
    })
}
modules.find = (req, res) => {
    const query = req.query,
        filter = query.filter || {},
        limit = query.limit,
        skip = query.skip;
        console.log('limit is :', limit, 'skip is : ', skip)
    Class.find(filter).limit(limit).skip(skip).then((classObj) => {
    	return res.json(response.success({data: classObj}))
    }).catch(err =>{
        console.log('class find err, err = ' + err);
        return res.json(response.error('查找班级失败')).end();
    })
}
modules.update = (req, res) => {
    const filter = req.query;
    const update = req.body;

    Class.update(filter, update).then((classObj) => {
    	return res.json(response.success({message: '更新班级成功', data: classObj}))
    }).catch(err => {
        console.log('class update err, err = ' + err);
        return res.json(response.error('更新班级失败')).end();
    })
}
modules.remove = (req, res) => {
    const filter = req.query;

    Class.remove(filter).then((result) => {
    	return res.json(response.success({message: '删除班级成功'}))
    }).catch(err => {
        console.log('class remove err, err = ' + err);
        return res.json(response.error('删除班级失败')).end();
    })
}

module.exports = (router) => {
    console.log('class file rooter now')
    crud(router, modules);
}